通过 new 和 delete 建立了一个脆弱的 “责任契约”。程序员必须精确预测每一个执行路径——包括提前返回和异常——以确保资源被正确释放。这种方法极易引发系统性故障。
系统性故障
- 泄漏陷阱: 在复杂的逻辑结构(if-else、switch)中,若未能为每个
new配对delete将随时间推移逐渐降低系统性能。 - 指针失效: 诸如 悬空指针 (在释放后访问内存)或 重复释放 会导致未定义行为和安全漏洞。
- 异常安全性: 如果在分配与释放之间发生异常,那么
delete将被完全跳过。
《 演进》
C++11/14 对 <memory> 头文件进行了重构,从‘裸指针’转向自动所有权模型,使安全性在编译期就得到强制保障。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Why is 'Exception Safety' a major concern with manual memory management?
Exceptions automatically call 'delete' for you.
An exception can bypass the 'delete' statement, causing a leak.
Exceptions only occur when memory is full.
Manual management prevents exceptions from being thrown.
✅ Correct!
If an exception is thrown before the program reaches 'delete', the stack unwinds and the heap pointer is lost without deallocation.❌ Incorrect
Standard pointers do not have destructors to clean up memory during an exception throw.QUESTION 2
What is a 'Dangling Pointer'?
A pointer that was never initialized.
A pointer still pointing to memory that has already been deleted.
A pointer that points to another pointer.
A pointer used in a circular dependency.
✅ Correct!
Accessing a dangling pointer causes 'Undefined Behavior' because the memory may have been reassigned.❌ Incorrect
Uninitialized pointers are 'wild' pointers; dangling pointers are 'stale' pointers to freed memory.QUESTION 3
Which header was significantly improved in C++11 and C++14 to automate memory?
✅ Correct!
The <memory> header contains std::unique_ptr, std::shared_ptr, and std::weak_ptr.❌ Incorrect
While smart pointers are the focus, the header name is simply <memory>.QUESTION 4
In the code example, what happens if size is 2048?
The memory is deleted before returning false.
The function crashes immediately.
Memory is allocated, but the function returns early without freeing it.
The compiler catches the leak at build time.
✅ Correct!
This is the 'Leakage Trap' where logic complexity leads to forgotten deallocations.❌ Incorrect
Compilers cannot reliably detect all runtime logic leaks in manual management.QUESTION 5
What is 'Double Freeing'?
Allocating two variables at once.
Attempting to delete the same memory address twice.
Using a smart pointer inside another smart pointer.
Freeing memory and setting the pointer to nullptr.
✅ Correct!
Double freeing often leads to heap corruption and can be exploited for security attacks.❌ Incorrect
Setting a pointer to nullptr after freeing is actually a safety best-practice.Legacy Server Audit
Resource Exhaustion Analysis
A high-frequency server uses a manual buffer (new char[1024]) for every incoming connection. The function returns 'false' early on socket timeouts without calling 'delete[]'.
Q
If 1,000 timeouts occur per minute, how much memory is leaked per hour?
Solution:
1,000 timeouts/min * 60 mins = 60,000 leaks/hour. At 1KB per leak, the system loses ~60MB of RAM per hour.
1,000 timeouts/min * 60 mins = 60,000 leaks/hour. At 1KB per leak, the system loses ~60MB of RAM per hour.
Q
What is the primary danger if this server runs for several days?
Solution:
Systemic memory exhaustion. Eventually, the OS will trigger the OOM (Out of Memory) killer or the process will crash because 'new' fails to allocate more memory.
Systemic memory exhaustion. Eventually, the OS will trigger the OOM (Out of Memory) killer or the process will crash because 'new' fails to allocate more memory.